<template>
	<div>
		<base-layout ref="layout" @pageChange="pageChange" :buttonList="buttonList" :showTitle="false"
			@onFuncBtn="getFuncBtn" :isPage="true">
			<div slot="main" slot-scope="{ tableHeight }" style="height: 100%;">
				<!-- <div class="mainTable"> -->
					<base-table ref="customtable" :border="true" :showIndex="false" :tabLoading.sync="tabLoading"
						:tableHeight="tableHeight" :tableData="tableData" :tableColumn="tableColumn" :slotrow="true"
						@radioChange="radioChange">
						<template v-slot:bm_CreateTime="{row}">
							<p>{{row.row.bm_CreateTime | timeSplit}}</p>
						</template>
					</base-table>
				<!-- </div> -->
			</div>
		</base-layout>
		<!-- 新增/编辑弹窗 -->
		<!-- <base-dialog :closeEscape="true" :showClose="true" :closeModal="false" :dialogVisible.sync="informationDialog"
			:footerShow="true" @handleConfirmClick="informationConfirmClick" class="userDialog" :title="selectStatus" width="95vw"
			top="1vh" @handleClose="handleClose">
			<document-information ref="informationFrom"></document-information>
		</base-dialog> -->
		<!-- 代码生成弹窗 -->
		<base-dialog :closeEscape="true" :showClose="true" :closeModal="false" :dialogVisible.sync="gnerationDialog"
			:footerShow="true" class="userDialog" title="代码生成" @update:dialogVisible="codeGeneration" @handleConfirmClick="codeSaving" width="95vw" top="1vh">
			<code-gneration ref="gnerationFrom"></code-gneration>
		</base-dialog>
		<!-- 编码设置窗 -->
		<base-dialog :closeEscape="true" :showClose="true" :closeModal="false" :dialogVisible.sync="orderDialog"
			:footerShow="true" @handleConfirmClick="orderConfirmClick" class="userDialog" title="编码设置" width="600px"
			top="10vh">
			<order-Setting ref="orderSettingFrom" :radioId="radioID"></order-Setting>
		</base-dialog>
		<!-- 单号设置弹窗 -->
		<base-dialog :closeEscape="true" :showClose="true" :closeModal="false" :dialogVisible.sync="permissionDialog"
			:footerShow="true" @handleConfirmClick="permissionConfirmClick" class="userDialog" title="数据权限设置"
			width="700px" top="10vh">
			<data-permission ref="permissionFrom"></data-permission>
		</base-dialog>
		<document-dialog ref="documentDialog" @handleClose="handleClose"></document-dialog>
	</div>
</template>

<script>
	import baseTable from "@/components/base/baseTable"
	import baseLayout from "@/components/base/baseLayout"
	import baseDialog from "@/components/base/BaseNewDialog/index.vue"
	import documentDialog from "./documentDialog"
	// import documentInformation from "./documentInformation"
	import dataPermission from "./dataPermission"
	import codeGneration from "./codeGneration"
	import orderSetting from "./orderSetting.vue"
	import configData from "./configData"
	import {
		GetBillList,
		GetBillData,
		GetTablesCoulmn,
		SaveData,
		GetBillDataPepedomSet,
		SaveBillDataPepedomSet,
		GetBillCodeSet,
		SaveBillCodeSet,
		DelMain,
		BillMachineCreateCode
	} from '@/api/apis/documentManagement'
	import {commonFun} from '@/api/apis/auth'
	import {
		getInfo,
	} from '@/utils/auth'
	export default {
		name: "review",
		components: {
			baseLayout,
			baseTable,
			baseDialog,
			// documentInformation,
			dataPermission,
			codeGneration,
			orderSetting,
			documentDialog
		},
		data() {
			return {
				documentDetails: [],
				innerVisible: false,
				informationDialog: false,
				gnerationDialog: false,
				orderDialog: false,
				permissionDialog: false,
				buttonList: [
					{
						'menuName': '代码生成',
						icon: 'el-icon-setting'
					},
					{
						'menuName': '单号设置',
						icon: 'el-icon-plus'
					},
					{
						'menuName': '数据权限设置',
						icon: 'icon-shoucang'
					},
				],
				tabLoading: false,
				tableData: [],
				settingCenter: "left",
				pageModel: {
					page: 1,
					limit: 20,
					Sequence: '',
					SequenceName: ''
				},
				radioIndex: false,
				radioID: '',
				bm_functionName: '',
				selectStatus:''
			}
		},
		computed: {
			// 表头配置
			tableColumn() {
				return configData.tableColumnJson
			},
		},
		watch: {
			informationDialog: {
				deep: true, //深度监听设置为 true
				handler: function(newV, oldV) {
					if (newV == false) {
						this.$nextTick(() => {
							this.$refs.informationFrom.$refs.customForm.choiceAssignment({})
							this.$refs.informationFrom.dataMain = {}
							this.$refs.informationFrom.tablesData = []
							this.$refs.informationFrom.tableCoulmnData = []
							this.$refs.informationFrom.temporaryTableData = []
							// this.$refs.orderSettingFrom.orderIndex = null
						})
					}
				}
			},
			orderDialog: {
				deep: true, //深度监听设置为 true
				handler: function(newV, oldV) {
					if (newV == false) {
						this.$nextTick(() => {
							this.$refs.orderSettingFrom.orderIndex = null
						})
					}
				}
			},

		},
		filters: {
			timeSplit(val) {
				if (!val) {
					return
				}
				let time = val.split("T")[0]
				return time
			},
		},
		mounted() {
			this.inPage()
		},
		methods: {
			inPage() {
				this.getTableData()
				let url = 'api/BillMachine/GetBillList'
				// this.runCommonFun(url)
			},
			async runCommonFun(url){
				let params = {
					...this.pageModel
				}
				let res = await commonFun(url,params)
				console.log(res)
			},
			// 获得表格数据
			async getTableData() {
				this.tabLoading = true
				let params = {
					...this.pageModel
				}
				// this.$refs.customtable.clearPageIndex()
				setTimeout(() => {
					// 方法区
					this.tabLoading = false
				}, 2000);
				let res = await GetBillList(params)

				if (res.code == 1) {
					this.$nextTick(() => {
						this.$refs.layout.setPageTotal(res.data[0])
					})
					this.tableData = res.data[1]
					this.tabLoading = false

				}

			},
			async informationGetBillData(id) {
				let params = {
					id
				}
				let res = await GetBillData(params)
				this.documentDetails = res.data
				if (res.code == 1) {
					// this.informationDialog = true
					if (this.selectStatus == "编辑") {
						this.$nextTick(() => {
							this.$refs.informationFrom.$refs.customForm.ruleForm = Object.assign({}, res.data[0])
							this.$refs.informationFrom.dataMain.Bm_IsApprover = res.data[0].Bm_IsApprover == 0 ?
								false : true
							this.$refs.informationFrom.dataMain.Bm_IsAttachment = res.data[0].Bm_IsAttachment ==
								0 ? false : true
							this.$refs.informationFrom.dataMain.Bm_openType = res.data[0].Bm_openType?parseInt(res.data[0].Bm_openType):''
							this.$refs.informationFrom.tablesData = Object.assign([], res.data[1])
							// this.$refs.informationFrom.$refs.customForm.ruleForm  = Object.assign({}, res.data[0])
						})
					} else if (this.selectStatus == "代码生成") {
						let selectAll = ["Bm_IsMenu","Bm_IsButton","Bm_IsCreateTable","Bm_IsEntity","Bm_IsController","Bm_IsView","Bm_IsJS"]
						let buildSelected = []
 							for(let key in res.data[0]){
 								if(selectAll.includes(key)){
									if (res.data[0][key] == 1) {
										buildSelected.push(key)
									}
								}
 						}
						this.$nextTick(() => {
							this.$refs.gnerationFrom.$refs.customForm.ruleForm = Object.assign({}, res.data[0])
							this.$refs.gnerationFrom.dataMain.Bm_IsApprover = res.data[0].Bm_IsApprover == 0 ?
								false : true
							this.$refs.gnerationFrom.dataMain.Bm_IsAttachment = res.data[0].Bm_IsAttachment ==
								0 ? false : true
							this.$refs.gnerationFrom.dataMain.Bm_openType = res.data[0].Bm_openType?parseInt(res.data[0].Bm_openType):''
							this.$refs.gnerationFrom.databaseTableData = Object.assign([], res.data[1])
							this.$refs.gnerationFrom.buildSelected = buildSelected
						})
					}
				}
			},
			// 获得数据权限详情
			async GetPermissionDetail(id) {
				let params = {
					id
				}
				let res = await GetBillDataPepedomSet(params)
				if (res.code == 1) {
					this.permissionDialog = true
					this.$nextTick(() => {
						this.$refs.permissionFrom.$refs.customForm.incomingParameters(res.data[0])
						this.$refs.permissionFrom.$refs.customForm.getField("bm_functionName", this
							.bm_functionName)
						this.$refs.permissionFrom.$refs.customForm.getField("id", id)
					})
				}
			},
			// 获取编码详情
			async getOrderDetail(id) {
				let params = {
					id
				}
				let res = await GetBillCodeSet(params)
				if (res.code == 1) {
					this.orderDialog = true
					this.$nextTick(() => {
						this.$refs.orderSettingFrom.orderTableData = Object.assign([], res.data[0])
						this.$refs.orderSettingFrom.formData.name = this.bm_functionName
					})
				}
			},
			// 关闭代码生成弹窗重置数据
			codeGeneration(){
				this.$refs.gnerationFrom.fieldTableData = []
				this.$refs.gnerationFrom.buildSelected = []
			},
			// 单选
			radioChange(val) {
				this.radioIndex = val.index
				this.radioID = val.bm_id
				this.bm_functionName = val.bm_functionName
			},
			// 查询、筛选事件
			pageChange(model) {
				this.radioIndex = false
				this.radioID = ''
				this.pageModel.page = model.page
				this.pageModel.limit = model.limit
				this.$refs.customtable.clearRadioIndex()
				this.getTableData()
			},
			// 按钮点击事件
			getFuncBtn(btnEven) {
				if (btnEven.menuName == '新增') {
					this.$refs.documentDialog.openDialog('新增')
				}
				if (btnEven.menuName == '编辑') {
					if (this.radioIndex === false) {
						this.$tipConfirm('请选择一条数据','warning').then(()=>{})
						return
					}
					this.$refs.documentDialog.openDialog('编辑',this.radioID)
					// this.informationGetBillData(this.radioID)
					// this.selectStatus = "编辑"
					// this.informationDialog  = true
				}
				if (btnEven.menuName == '代码生成') {
					if (this.radioIndex === false) {
						this.$vmNews('请选择一条数据！')
						return
					}
					this.informationGetBillData(this.radioID)
					this.selectStatus = "代码生成"
					this.gnerationDialog = true
				}
				if (btnEven.menuName == '单号设置') {
					if (this.radioIndex === false) {
						this.$vmNews('请选择一条数据！')
						return
					}
					this.getOrderDetail(this.radioID)

				}
				if (btnEven.menuName == '数据权限设置') {
					if (this.radioIndex === false) {
						this.$vmNews('请选择一条数据！')
						return
					}
					this.GetPermissionDetail(this.radioID)
				}
				if (btnEven.menuName == '删除') {
					this.delData()
				}

			},
			delData(){
				if (this.radioIndex === false) {
					this.$vmNews('请选择一条数据！')
					return
				}
				this.$delConfirm().then((res)=>{
					let params = {
						id:this.radioID,
					}
					DelMain(params).then(res=>{
						if(res.code == 1){
							this.$vmNews('删除成功！','success')
							this.resetTable()
						}
					})
				})
			},
			// 数据权限保存
			async permissionConfirmClick() {
				let ruleForm = this.$refs.permissionFrom.$refs.customForm.ruleForm
				let params = {
					id: this.radioID,
					dataMain: {
						...ruleForm
					}
				}
				let res = await SaveBillDataPepedomSet(params)
				if (res.code == 1) {
					this.$vmNews('数据权限保存成功', 'success')
					this.resetTable()
					this.permissionDialog = false
				}
			},
			// 编码设置保存
			async orderConfirmClick() {
				let RuleStr = this.$refs.orderSettingFrom.formData.RuleStr
				let ruleData = this.$refs.orderSettingFrom.orderTableData
				let params = {
					id: this.radioID,
					RuleStr: RuleStr,
					ruleData: ruleData
				}
				let res = await SaveBillCodeSet(params)
				if (res.code == 1) {
					this.$vmNews('编码设置保存成功', 'success')
					this.resetTable()

					this.orderDialog = false
				}
			},
			informationConfirmClick() {
				let ruleForm = this.$refs.informationFrom.$refs.customForm.ruleForm //单据信息
				let dataMain = this.$refs.informationFrom.dataMain
				let temporaryTableData = this.$refs.informationFrom.temporaryTableData // 已点击
				let tableCoulmnData = []
				if (temporaryTableData.length > 0) {
					temporaryTableData.forEach(el => {
						el.tableData.forEach(item => {
							item.tableNameEN = el.name
							tableCoulmnData.push(item)
						})
					})
				}
				if (dataMain.Bm_IsApprover) {
					dataMain.Bm_IsApprover = 1
				} else {
					dataMain.Bm_IsApprover = 0
				}
				if (dataMain.Bm_IsAttachment) {
					dataMain.Bm_IsAttachment = 1
				} else {
					dataMain.Bm_IsAttachment = 0
				}
				dataMain.Bm_CreatePersonID = JSON.parse(getInfo()).personID
				let params = {
					dataMain: {
						...ruleForm,
						...dataMain
					},
					tablesData: this.$refs.informationFrom.tablesData,
					tableCoulmnData:tableCoulmnData
				}
				let arr = this.$refs.informationFrom.tablesData.filter((el) => { 
					if (this.$refs.informationFrom.documentRecord.length > 0) {
						for (let i = 0; i < this.$refs.informationFrom.documentRecord.length; i++) {
							return el.tableNameEN != this.$refs.informationFrom.documentRecord[i].tableNameEN
						}
					} else {
						return el
					}
				})
				let dataCollection = []
				console.log(arr,"arr");
				if (arr.length > 0) { 
					arr.forEach(el => {
						GetTablesCoulmn({tableNameEN: el.tableNameEN}).then(res => {
							dataCollection.push.apply(dataCollection,res.data[0])
						})
					})
				}
				setTimeout(() => {
					params.tableCoulmnData.push.apply(params.tableCoulmnData,dataCollection)
					SaveData(params).then(res => {
						this.$vmNews(res.message,res.success === "true"?"success":'error')
						if(res.success === "true"){
							this.$refs.informationFrom.documentRecord = []
							this.informationDialog = false
							this.resetTable()
					}})
				},1500)
			},
			handleClose(){
				// this.$refs.informationFrom.documentRecord = []
				this.resetTable()
			},
			// 
			async codeSaving() {
				console.log(666);
				// console.log(this.radioID);
				let dataAll = {
					IsCreateMenuNew: 0,
					IsCreateButtonNew: 0,
					IsCreateTableNew: 0,
					IsEntityNew: 0,
					IsControllerNew: 0,
					IsJSNew: 0,
					IsViewNew: 0
				}
				let data = this.$refs.gnerationFrom.needToSelect
				for(let key in dataAll) {
					if(data.includes(key)){
						dataAll[key] = 1
					}
				}
				let res = await BillMachineCreateCode({
					Bm_id: this.radioID,
					...dataAll,
				})
				this.gnerationDialog = false
				this.$refs.gnerationFrom.needToSelect = []
				this.$refs.gnerationFrom.fieldTableData = []
				this.$refs.gnerationFrom.buildSelected = []
				if (res.code ==1) {
					this.$vmNews('代码生成成功!</br>'+res.data[0])
				} else if (res.success =="false") {
					let title = res.message
					this.$message.error(res.message);
				}
			},
			resetTable() {
				this.radioIndex = false
				this.radioID = ''
				this.pageModel.page = 1
				this.getTableData()
				this.$nextTick(() => {
					this.$refs.layout.setPageNum(1)
					this.$refs.customtable.clearRadioIndex()
				})
			}
		},
	}
</script>

<style scoped lang="scss">
	>>>.el-dialog--center .el-dialog__body {
		padding-top: 0 !important;
	}
</style>
